package com.zlk.algorithm.algorithm.sort.basesSimp;

import com.zlk.algorithm.algorithm.sort.SortUtils;
import org.junit.Test;

import java.util.Arrays;

/**
 * @program: algorithm
 * @ClassName InsertionSort
 * @description: 最差时间复杂度位 n的平方   最好的位n   时间复杂度应该按照  最差来估计
 * @author: slfang
 * @create: 2023-12-18 18:57
 * @Version 1.0
 **/
public class InsertionSort {

    @Test
    public void test(){
        int testTimes = 100000;
        int randomLength = 20;
        int randomVal = 100;

        for (int i = 0; i < testTimes; i++) {
            int[] randomArr1 = SortUtils.getRandomArr(randomVal, randomLength);
            int[] copy = SortUtils.copyArray(randomArr1);
            SortUtils.comparator(copy);
            test(randomArr1);
            if(!SortUtils.isEqual(copy,randomArr1)){
                System.out.println("第"+(i+1)+"comparator测试失败"+ Arrays.toString(copy));
                System.out.println("第"+(i+1)+"insertionSort测试失败"+ Arrays.toString(randomArr1));
                return;
            }

        }
        System.out.println("success !!!");
    }


    public void insertionSort(int[] arr){
        if(arr==null||arr.length<2){
            return ;
        }

        for (int i = 1; i < arr.length ; i++) {
            int j = i;
            while (j>0&&arr[j]<arr[j-1]){
                SortUtils.swap(arr,j,j-1);
                j--;
            }
        }

    }



    public void test(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            int temp = i;
            while (temp>0&&arr[temp]<arr[temp-1]){
                SortUtils.swap(arr,temp,temp-1);
                temp--;
            }
        }

    }


















}
